package cn.com.pansky.action.bank;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.pansky.payment.PayConf;
import cn.com.pansky.util.DateUtil;

import com.psbc.payment.client.SignatureService;

/**
 * 邮储银行支付缴费
 */
public class PsbcPayment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PsbcPayment() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("deprecation")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String transName = "WPER"; // 交易名称
		// 交易数据明文数据封装开始
		String TranAbbr = "WPER"; // 交易缩写   必填
		if(request.getParameter("payType").equals("1")) {
			transName = "IPER"; // web端支付
			TranAbbr = "IPER";
		}
		String MercDtTm = DateUtil.currentTime(); // 商户日期时间 必填
		// 订单号+mmssSSS组成 TODO 邮储支付订单号在生成订单时已经处理
//		String TermSsn = request.getParameter("orderNumber")+DateUtil.current7Time(); // 订单号 必填
		String TermSsn = request.getParameter("orderNumber"); // 订单号 必填
		// 将新订单号存入session中，用户支付成功后退回用
		request.getSession().setAttribute("orderNumberYC", TermSsn);
		String OSttDate = ""; // 原交易商户日期
		String OAcqSsn = ""; // 原交易订单号
		// 商户号 必填  测试数据：1100529310001000003
		// 正式商户号(扶风县)：1100529310001000360
		String MercCode = request.getSession().getAttribute("AAE051").toString();
		String TermCode = ""; // 终端号
		String TranAmt = request.getParameter("orderAmount"); // 交易金额 必填
		String MercUrl = PayConf.MercUrl; // 商户通知地址
		String Remark1 = ""; // 备注1
		String Remark2 = ""; // 备注2
		String LimitTime = ""; // 有效时间
		// 交易数据明文数据封装结束
		// 交易数据明文
		String Plain = "TranAbbr="+TranAbbr+"|MercDtTm="+MercDtTm+"|TermSsn="+TermSsn+"|OSttDate="
				+OSttDate+"|OAcqSsn="+OAcqSsn+"|MercCode="+MercCode+"|TermCode="+TermCode
				+"|TranAmt="+TranAmt+"|MercUrl="+URLEncoder.encode(MercUrl)+"|Remark1="+Remark1+"|Remark2="
				+Remark2+"|LimitTime="+LimitTime;
		
		// 交易数据明文签名
		String Signature = SignatureService.sign(Plain);
		// 构造请求数据表单
		StringBuffer formbuf = new StringBuffer();
		formbuf.append("\r\n<script language=\"javascript\">window.onload=function(){document.payform.submit();}</script>\r\n");
		formbuf.append("\r\n<form id='payform' name='payform' action='"+PayConf.PayUrl+"' method='POST'>\r\n");
		formbuf.append("\t<input type='hidden' name='transName' value='" + transName + "' />\r\n");
		formbuf.append("\t<input type='hidden' name='Plain' value='" + Plain + "' />\r\n");
		formbuf.append("\t<input type='hidden' name='Signature' value='" + Signature + "' />\r\n");
		formbuf.append("</form>\r\n");
		response.setContentType("text/html;charset=" + "UTF-8");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().write(formbuf.toString().replace('\'', '"'));
		} catch (IOException e) {
			System.out.println("支付失败");
		}
	}
}
